<?php
/**
 * Copyright (c) 2006-2008, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @link         http://feather-cm.googlecode.com/
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class ContactController extends AppController
{
	public $_templates = 'contact';
	
	/**
	 * Contacts site editors.
	 */
	function index()
	{
		if ($this->__sendEmail())
		{
			$this->message(_('Message sent!'), _("Thank you very much for your email, we appreciate it."));
			exit;
		}
		$this->pageTitle = _('Contact us');
	}
	
	/**
	 * Contacts a member.
	 * 
	 * @param $member_id String
	 */
	function member($member_id)
	{
		if (!empty($member_id))
		{
			# does member exists?
			$Member = $this->load->model('Member');
			$allow_contact = $Member->field('allow_contact', $member_id);
			
			if ($allow_contact === false)
				$this->flash(_('Error: no such member.'), getReferer(), 'error');
			
			if (!$allow_contact and !$this->Auth->inGroup(Core::admins))
				$this->flash(sprintf(_('Error: %s refuses to be contacted by email.'), $member_id), getReferer(), 'error');
			
			$this->set('member_id', $member_id);
			$this->set('allow_contact', $allow_contact);
		}
		
		if ($this->__sendEmail($member_id))
		{
			$this->message(_('Message sent!'), sprintf(_("%s should receive your message shortly."), $member_id));
			exit;
		}
		$this->pageTitle = sprintf(_('Send an email to %s'), $member_id);
	}
	
	/**
	 * Checks the form and sends the email.
	 */
	private function __sendEmail($member_id=null)
	{
		if (HTTP::isPost() and !empty($this->data))
		{
			$Member = $this->load->model('Member');
			
			if (!$this->Auth->identified)
			{
				# stupid captcha for stupid spam bots
				if (!isset($this->data['Contact']['user_site_f'])
					or !empty($this->data['Contact']['user_site_f']))
				{
					Error::show(403);
				}
				
				# let's get paranoid!
				$this->Contact->paranoia = true;
			}
			else
			{
				# let's get sender's name and email
				$this->data['Contact']['name']  = $this->Auth->id;
				$this->data['Contact']['email'] = $Member->field('email', $this->Auth->id);
			}
			
			# is form ok?
			if ($this->Contact->validates())
			{
				$this->load->component('Swift');
				
				# recipients
				if (isset($this->data['Contact']['copy']))
					$this->Swift->addRecipient($this->data['Contact']['email'], $this->data['Contact']['name'], 'Cc');
				
				# recipient is website's contact or a member?
				if (empty($member_id))
					$this->Swift->addRecipient($this->Config->website['email'], $this->Config->website['name']);
				else
					$this->Swift->addRecipient($Member->field('email', $member_id, -1), $member_id);
				
				# sends
				$subject = empty($this->data['Contact']['subject']) ?
					sprintf(_('Message from %s'), $this->data['Contact']['name']) :
					$this->data['Contact']['subject'];
				
				$mail_from = array($this->data['Contact']['email'], $this->data['Contact']['name']);
				$this->Swift->send($subject, &$mail_from);
				return true;
			}
		}
		return false;
	}
}
?>